import { request } from '../../../services/_utils/util';
const api = require('../../../services/config/api');
import Toast from '@vant/weapp/toast/toast';
import Notify from '@vant/weapp/notify/notify';
Page({
  data: {
    istrue: true,
    display: 'none',
    display2: 'inline-black',
    seconds: 59,
    formdata: {},
    setpassword: false,
  },

  yzmlogin() {
    console.log(123);
    this.setData({
      istrue: false,
      setpassword: false,
    });
  },
  pasdlogin() {
    console.log(456);
    this.setData({
      istrue: true,
      setpassword: false,
    });
  },
  timeChange(e) {
    //倒计时改变
    this.setData({
      seconds: e.detail.seconds,
    });
  },
  timeFinish(e) {
    //倒计时结束
    const countDown = this.selectComponent('.control-count-down');
    countDown.reset();
    this.setData({
      display: 'none',
      display2: 'inline-black',
    });
  },
  //获取验证码
  getYzm() {
    console.log(this.data.formdata);
    var that = this;
    if (
      this.data.formdata.phone == null ||
      this.data.formdata.phone.trim() == '' ||
      this.data.formdata.phone == undefined
    ) {
      Notify({ type: 'danger', message: '请填写手机号' });
      return false;
    }
    Toast.loading({
      forbidClick: true,
      loadingType: 'spinner',
      duration: 0,
    });
    this.setData({
      'formdata.isNotcreate': true,
    });
    request(api.smsyzm + '?phone=' + this.data.formdata.phone + '&isNotcreate=' + true, {}, 'post').then(function (
      res,
    ) {
      console.log(res);
      Toast.clear();
      if (res.code == 200) {
        Notify({ type: 'success', message: '发送成功' });
        that.setData({
          display: 'inline-black',
          display2: 'none',
        });
        const countDown = that.selectComponent('.control-count-down');
        countDown.start();
      } else {
        Notify({ type: 'danger', message: res.msg });
      }
    });
  },
  //  忘记密码/设置密码
  forgetsetpassword() {
    this.setData({
      istrue: false,
      setpassword: true,
    });
  },
  onChange1(e) {
    this.setData({
      'formdata.phone': e.detail.trim(),
    });
  },
  onChange2(e) {
    this.setData({
      'formdata.password': e.detail.trim(),
    });
  },
  onChange3(e) {
    this.setData({
      'formdata.surepassword': e.detail.trim(),
    });
  },
  onChange4(e) {
    this.setData({
      'formdata.yzmcode': e.detail.trim(),
    });
  },
  //登录
  login() {
    //判断验证码登录还是密码登录
    console.log(this.data.istrue);
    if (this.data.istrue) {
      //密码登录
      console.log(this.data.formdata.password);
      if (
        this.data.formdata.password == null ||
        this.data.formdata.password.trim() == '' ||
        this.data.formdata.password == undefined
      ) {
        Notify({ type: 'danger', message: '请输入密码' });
        return false;
      }
    } else {
      //验证码登录
      if (
        this.data.formdata.yzmcode == null ||
        this.data.formdata.yzmcode.trim() == '' ||
        this.data.formdata.yzmcode == undefined
      ) {
        Notify({ type: 'danger', message: '请输入验证码' });
        return false;
      }
    }
    if (
      this.data.formdata.phone == null ||
      this.data.formdata.phone.trim() == '' ||
      this.data.formdata.phone == undefined
    ) {
      Notify({ type: 'danger', message: '请输入手机号' });
      return false;
    }
    this.setData({
      'formdata.ispasswordlogin': this.data.istrue,
    });
    console.log(this.data.formdata);
    Toast.loading({
      forbidClick: true,
      loadingType: 'spinner',
      duration: 0,
    });
    request(api.AuthLoginByaccount, this.data.formdata, 'post').then(function (res) {
      console.log(res);
      Toast.clear();
      if (res.code == 200) {
        Notify({ type: 'danger', message: '登录成功' });
        wx.setStorageSync('userInfo', res.data);
        wx.setStorageSync('token', res.data.token);
        //跳转到个人信息页
        wx.redirectTo({ url: '/pages/usercenter/index?currAuthStep=2' });
      } else {
        Notify({ type: 'danger', message: res.msg });
      }
    });
  },
  //设置密码
  setpas() {
    var that = this;
    if (
      this.data.formdata.surepassword == null ||
      this.data.formdata.surepassword.trim() == '' ||
      this.data.formdata.surepassword == undefined
    ) {
      Notify({ type: 'danger', message: '请输入密码' });
      return false;
    }
    //验证码登录
    if (
      this.data.formdata.yzmcode == null ||
      this.data.formdata.yzmcode.trim() == '' ||
      this.data.formdata.yzmcode == undefined
    ) {
      Notify({ type: 'danger', message: '请输入验证码' });
      return false;
    }
    if (
      this.data.formdata.phone == null ||
      this.data.formdata.phone.trim() == '' ||
      this.data.formdata.phone == undefined
    ) {
      Notify({ type: 'danger', message: '请输入手机号' });
      return false;
    }
    this.setData({
      'formdata.isupdate': true,
    });
    Toast.loading({
      forbidClick: true,
      loadingType: 'spinner',
      duration: 0,
    });
    request(api.updateUser, this.data.formdata, 'post').then(function (res) {
      Toast.clear();
      if (res.code == 200) {
        Notify({ type: 'danger', message: '设置成功,请前往登录' });
        that.setData({
          formdata: {},
        });
      } else {
        Notify({ type: 'danger', message: res.msg });
      }
    });
  },
});
